<?php
session_start();
require_once("database.php");
$conn=mysql_link();
$name=$_SESSION['username'];
//echo $name;
$us=$_SESSION['email'];
$cha="SELECT * FROM users where Email='$us'";
$sql2= mysqli_query($conn,$cha);
$res2= mysqli_fetch_assoc($sql2);
$username = $res2["Name"];
$userid=$res2['UserID'];
//echo $userid;
//print_r($name)

$cu="SELECT * FROM borrowrecords where UserID='$userid' and re=1";
$cu1= mysqli_query($conn,$cu);
$cures=mysqli_num_rows($cu1);
//print_r($cures);
$data=date("Y-m-d H:i:s");
$end=date('Y-m-d H:i:s', strtotime('+15day'));
if ($_GET["action"]=="jieyue"){
    $user=$_POST['user'];
    $bookid=$_POST['bookid'];
    $number=$_POST['number'];
    $bookname=$_POST['bookname'];
    if ($cures<5){
        if (empty($user and $bookid and $number and $bookname)){
            $book="SELECT * FROM books where BookID='$bookid'";
            $book1= mysqli_query($conn,$book);
            $book2=mysqli_fetch_assoc($book1);
            $boname=$book2['Title'];
            $di=$book2['di'];
            $paper=$book2['Author'];
            $borrow="INSERT INTO borrowrecords (UserID,BookID,BorrowDate,ReturnDate,re,di,Bookname,paper) values ($userid,$bookid,'$data','$end',1,'$di','$boname','$paper')";
            $borrow1= mysqli_query($conn,$borrow);
            echo "<script> alert('借阅成功')</script>";
        }else{
            echo "<script> alert('请填写借阅信息')</script>";
        }
    }else{
        echo "<script> alert('借阅以到达上限')</script>";
    }

}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/bookBorrow.css">
    <link href="https://unpkg.com/boxicons@2.1.4/css/boxicons.min.css" rel="stylesheet" />
    <!-- <script src="../js/Public Section.js"></script> -->
</head>

<body>

    <!--背景-->
    <div class="all">
    <!-- 左手边菜单栏 -->
        <div class="h left_Menu_Bar Center">
            <div class="Menu_Bar Center">
                <!-- 头像 -->
                <div class="avatar">
                    <!-- 默认头像 -->
                    <h1>读</h1>
                </div>
                <!-- 菜单 -->
                <div class="Menu Font">
                    <ul class="Center ct">
                        <li><a href="#">用户管理</a></li>
                        <li>
                            <a href="#">借阅管理</a> <button class="btn" id="btn"><i class="bx bx-chevron-down"
                                    id="arrow"></i></button>
                        </li>
                        <!-- 下拉菜单 -->
                        <div class="box " id="dropdown">
                            <ul class="drop_down_menu Center">
                                <li><a href="../html/borrow.html">借阅</a></li>
                                <li><a href="../html/return.html">归还</a></li>
                                <li><a href="#">记录</a></li>
                                <li><a href="../html/expire.html">过期</a></li>
                            </ul>
                        </div>

                        <li><a href="#">图书列表</a></li>

                    </ul>
                </div>
            </div>
        </div>
        <!-- <div class="h bodyed"> -->
        <!-- 白色背景 -->
        <div class="Background">
             <div class="top">
            <h1 class="title">图书借阅</h1>
            <div class="images">
                <img src="../image/搜索.png" alt="">
                <img src="../image/问号.png" alt="">
            </div>
        </div>
            <form action="?action=jieyue" id="form" method="post">
                <div class="all_inputs">
                    <div class="inputs">
                        <div>用户名<input name="user" class="userName" id="myInput" autofocus type="text" disabled value="<?echo $username?> "></div>
                        <div>索书号<input name="bookid" class="bookId" id="myInput" autofocus type="text" ></div>
                        <div>条形码<input name="number" class="Number" id="myInput" autofocus type="text" ></div>
                        <div>书名<input name="bookname" class="bookName" id="myInput" autofocus type="text"></div>
                    </div>
                    <div class="bottoms">
                        <div class="yes">
                            <input type="submit" class="yesser" value="确认" onclick="submit()">
                        </div>
                    </div>
                </div>
            </form>
            <div class="not">
                <input type="submit" id="clearInput" value="取消">

            </div>
        </div>
    </div>
</div>
</body>

</html>
<script src="../js/bookBorrow.js"></script>